var map;
var layer;
var WGS_PROJECTION = new OpenLayers.Projection("EPSG:4326");
var SPHERICAL_MERCATOR_PROJECTION = new OpenLayers.Projection("EPSG:900913");
var features = new Array();
var points;
var f;
var tolerance = 500;

function init() {
	map = new OpenLayers.Map("map", {
		displayProjection: WGS_PROJECTION
	});
}

function add_layer(layerName){
    if(layer)
        map.removeLayer(map.layers[0])
	
    layer = new OpenLayers.Layer.Vector(layerName, {});
    map.addLayer(layer);
}

//in home.html
function add_reports(reports) {
	var format = new OpenLayers.Format.GeoJSON({internalProjection: SPHERICAL_MERCATOR_PROJECTION, externalProjection: WGS_PROJECTION});
	layer.addFeatures(format.read(reports));
}

function get_number_of_nearby_points(point){
    nearby_points = 0;
    for(var i=0; i<points.length; i++){
        distance = point.distanceTo(points[i])
        if(distance!=0 && distance <= tolerance)
            nearby_points++;
    }
    return nearby_points;
}

function get_space_difference(point){
    total_distance = 0;
    //number of points within radius
    number_of_points = 0;
    
    for(var i=0; i<points.length; i++){
        distance = point.distanceTo(points[i])
        if(distance!=0 && distance <= tolerance){
            number_of_points++;
            total_distance += distance;
        }
    }
    if(number_of_points)
        return total_distance/number_of_points;
    else
        return 0;
}

function get_index_difference(point, point_index){
    total_index_difference = 0;
    //number of points within radius
    number_of_points = 0;
    //points within radius
    nearby_points = new Array();
    nearby_features = new Array();
    
    //determine nearby points
    for(var i=0; i<points.length; i++){
        distance = point.distanceTo(points[i])
        if(distance!=0 && distance <= tolerance){
            nearby_points.push(i)
        }
    }
    
    //get features corresponding to nearby points
    for(var i=0; i<nearby_points.length; i++){
        nearby_features.push(features[nearby_points[i]]);
    }
    
    //compute total index difference for all nearby points
    for(var i=0; i<nearby_features.length; i++){
        difference = Math.abs(nearby_features[i].attributes.index - point_index) 
        total_index_difference += difference;
    }
    
    number_of_points = nearby_features.length
    if(number_of_points)
        return total_index_difference/number_of_points;
    else
        return 0;
}

function get_time_difference(point, point_date_time){
    total_time_difference = 0;
    //number of points within radius
    number_of_points = 0;
    //points within radius
    nearby_points = new Array();
    nearby_features = new Array();
    
    //determine nearby points
    for(var i=0; i<points.length; i++){
        distance = point.distanceTo(points[i])
        if(distance!=0 && distance <= tolerance){
            nearby_points.push(i)
        }
    }
    
    //get features corresponding to nearby points
    for(var i=0; i<nearby_points.length; i++){
        nearby_features.push(features[nearby_points[i]]);
    }
    
    //compute total time difference for all nearby points
    for(var i=0; i<nearby_features.length; i++){
        a = stringToDate(nearby_features[i].attributes.date_event);
        b = stringToDate(point_date_time);
        difference_in_milliseconds = Math.abs(a.getTime() - b.getTime()); 
        days = Math.ceil(difference_in_milliseconds / 1000 / 60 / 60 / 24);
        total_time_difference += days;
    }
    
    number_of_points = nearby_features.length
    if(number_of_points)
        return total_time_difference/number_of_points;
    else
        return 0;
}

function stringToDate(s) {
  var dateParts = s.split(' ')[0].split('-'); 
  var timeParts = s.split(' ')[1].split(':');
  var d = new Date(dateParts[0], --dateParts[1], dateParts[2]);
  d.setHours(timeParts[0], timeParts[1], timeParts[2]);

  return d;
}

function prepare_points(){
    features = map.layers[0].features;
    points = new Array();
    
    //determine points within that layer
    for(var i=0; i<features.length; i++){
        points.push(features[i].geometry);
    }
}

function validate_reports(){
    prepare_points();
    f = new Array();
    
    for(var i=0; i<features.length; i++){
        var validation_details = new Array();
        
        num_points = get_number_of_nearby_points(points[i]);
        validation_details.push(num_points);
        
        space_difference = get_space_difference(points[i]);
        validation_details.push(space_difference);
        
        time_difference = get_time_difference(points[i], features[i].attributes.date_event);
        validation_details.push(time_difference);
        
        index_difference = get_index_difference(points[i], features[i].attributes.index);
        validation_details.push(index_difference);
        
        f.push(validation_details)
    }
}

function display_reports(){
    var info_div = document.getElementById('reports');
    info_div.innerHTML = '';
    
    for(var i=0; i<f.length; i++){
        info_div.innerHTML +=   '<div class="report">' +
                                '<div>nearby points: ' + f[i][0] + '</div>' +
                                '<div>space difference: ' + f[i][1].toFixed(2) + ' meters</div>' +
                                '<div>time difference: ' + f[i][2] + ' days</div>' +
                                '<div>index difference: ' + f[i][3].toFixed(2) + '</div>' +
                                '</div>';
    }
}




